﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace WhittleList.Helpers
{
    public static class HtmlHelpers
    {
        public static string GetClassFromIndex(this HtmlHelper html, int index, params string[] args)
        {
            return args[index % args.Length];
        }

        public static string DeleteLink(this HtmlHelper html, string linkText, object routeValues)
        {
            return html.DeleteLink(linkText, "Delete", html.ViewContext.RouteData.GetRequiredString("controller"), routeValues);
        }

        public static string DeleteLink(this HtmlHelper html, string linkText, string controler, object routeValues)
        {
            return html.DeleteLink(linkText, "Delete", controler, routeValues);
        }

        public static string DeleteLink(this HtmlHelper html, string linkText, string action, string controler, object routeValues)
        {
            var urlHelper = new UrlHelper(html.ViewContext.RequestContext);

            string url = urlHelper.Action(action, controler, routeValues);

            string format = @"<form method=""post"" action=""{0}"" class=""delete-link"" onsubmit=""return confirm('Are you html sure?');""><input type=""submit"" value=""{1}"" />{2}</form><a class=""delete-link"" style=""display:none;"" href=""{0}"">{1}</a>";

            string form = string.Format(format, html.AttributeEncode(url)
              , html.AttributeEncode(linkText)
              , html.AntiForgeryToken());
            //return form + html.RouteLink(linkText, routeName, routeValues, new { @class = "delete-link", style = "display:none;" });
            return form;
        }


        public static MvcDiv OneColumnDivLeft(this HtmlHelper html)
        {
            return html.OneColumnDivLeft(null);
        }

        public static MvcDiv OneColumnDivLeft(this HtmlHelper html, object htmlAttributes)
        {
            return html.OneColumnDivLeft(new RouteValueDictionary(htmlAttributes));
        }

        public static MvcDiv OneColumnDivLeft(this HtmlHelper html, IDictionary<string, string> htmlAttributes)
        {
            return html.GetDiv(htmlAttributes, "colOne");
        }
        
        public static MvcDiv TwoColumnDivLeft(this HtmlHelper html)
        {
            return html.TwoColumnDivLeft(null);
        }

        public static MvcDiv TwoColumnDivLeft(this HtmlHelper html, object htmlAttributes)
        {
            return html.TwoColumnDivLeft(new RouteValueDictionary(htmlAttributes));
        }

        public static MvcDiv TwoColumnDivLeft(this HtmlHelper html, IDictionary<string, string> htmlAttributes)
        {
            return html.GetDiv(htmlAttributes, "colTwo");
        }

        public static MvcDiv TwoColumnDivRight(this HtmlHelper html)
        {
            return html.TwoColumnDivRight(null);
        }

        public static MvcDiv TwoColumnDivRight(this HtmlHelper html, object htmlAttributes)
        {
            return html.TwoColumnDivRight(new RouteValueDictionary(htmlAttributes));
        }

        public static MvcDiv TwoColumnDivRight(this HtmlHelper html, IDictionary<string, string> htmlAttributes)
        {
            return html.GetDiv(htmlAttributes, "colTwo colRight");
        }

        private static MvcDiv GetDiv(this HtmlHelper html, IDictionary<string, string> htmlAttributes, string className)
        {
            TagBuilder tagBuilder = new TagBuilder("div");
            tagBuilder.MergeAttributes(htmlAttributes);
            // action is implicitly generated, so htmlAttributes take precedence.
            tagBuilder.MergeAttribute("class", className);


            HttpResponseBase httpResponse = html.ViewContext.HttpContext.Response;
            httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag));
            return new MvcDiv(html.ViewContext.HttpContext.Response);
        }

        public static void EndDiv(this HtmlHelper html)
        {
            HttpResponseBase httpResponse = html.ViewContext.HttpContext.Response;
            httpResponse.Write("</div>");
        }

    }

    public class MvcDiv : IDisposable
    {
        private bool _disposed;
        private readonly HttpResponseBase _httpResponse;

        public MvcDiv(HttpResponseBase httpResponse)
        {
            if (httpResponse == null) {
                throw new ArgumentNullException("httpResponse");
            }
            _httpResponse = httpResponse;
        }
        public void Dispose()
        {
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;
                _httpResponse.Write("</div>");
            }
        }

        public void EndDiv()
        {
            Dispose(true);
        }
    }
}
